"""
Case Type   : fsync
Case Name   : gs_guc reload方式设置参数fsync为off，设置成功
Create At   : 2023/8/16
Owner       : li-xin12345
Description :
    1、查询fsync默认值
    2、fsync为sighup级别参数，使用gs_guc reload方式设置参数fsync为off
    3、执行简单的DML操作
    4、恢复fsync默认值
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class FsyncTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.tb_name = 't_china_mobile_0002'

    def test_fsync(self):
        text = '-----step1:查询fsync默认值;expect:成功-----'
        self.log.info(text)
        self.default_param = self.common.show_param('fsync')
        self.log.info(self.default_param)
        self.assertIn('on', self.default_param, '执行失败：' + text)

        text = '-----step2:fsync为sighup级别参数，' \
               '使用gs_guc reload方式设置参数fsync为off;expect:成功-----'
        self.log.info(text)
        res = self.pri_sh.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, 'fsync=off')
        self.log.info(res)
        self.assertTrue(res)

        text = '-----step3:执行简单的DML操作;expect:成功-----'
        self.log.info(text)
        sql = f'''drop table if exists {self.tb_name} cascade;
        create table {self.tb_name}(v1 int, v2 char(20));
        insert into {self.tb_name} values(generate_series(0, 100000), 'fsync');
        update {self.tb_name} set v2 = 'test' where v1 < 20000;
        select count(*) from {self.tb_name};
        delete from {self.tb_name};
        drop table if exists {self.tb_name} cascade;
        '''
        sql_res = self.pri_sh.execut_db_sql(sql)
        self.log.info(sql_res)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, sql_res,
                      '执行失败：' + text)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, sql_res,
                      '执行失败：' + text)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, sql_res,
                      '执行失败：' + text)
        self.assertIn(self.constant.UPDATE_SUCCESS_MSG, sql_res,
                      '执行失败：' + text)
        self.assertIn(self.constant.DELETE_SUCCESS_MSG, sql_res,
                      '执行失败：' + text)
        self.assertEqual(sql_res.count(self.constant.DROP_TABLE_SUCCESS), 2,
                         '执行失败：' + text)
        self.assertNotIn('ERROR', sql_res, '执行失败：' + text)

    def tearDown(self):
        text = '-----step4:恢复fsync默认值;expect:成功-----'
        self.log.info(text)
        drop_res = self.pri_sh.execut_db_sql(
            f'drop table if exists {self.tb_name} cascade;')
        self.log.info(drop_res)
        curr_param = self.common.show_param('fsync')
        if curr_param != self.default_param:
            res = self.pri_sh.execute_gsguc(
                'reload', self.constant.GSGUC_SUCCESS_MSG,
                f'fsync={self.default_param}')
            self.log.info(res)
            self.assertTrue(res)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, drop_res,
                      '执行失败：' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')